package schedule

import (
	"fmt"
	"sync"
)

// SchedulerManager 调度器管理器
type SchedulerManager struct {
	dogeScheduler *DogeTradeScheduler
	trxScheduler  *TrxTradeScheduler
	mu            sync.RWMutex
}

// NewSchedulerManager 创建新的调度器管理器
func NewSchedulerManager() *SchedulerManager {
	return &SchedulerManager{
		dogeScheduler: NewDogeTradeScheduler(),
		trxScheduler:  NewTrxTradeScheduler(),
	}
}

// StartDogeScheduler 启动DOGE交易调度器（默认智能模式）
func (sm *SchedulerManager) StartDogeScheduler() error {
	return sm.StartDogeSchedulerWithConfig(DogeSchedulerConfig{
		GridMode: GridModeSmart,
	})
}

// StartDogeSchedulerWithConfig 使用配置启动DOGE交易调度器
func (sm *SchedulerManager) StartDogeSchedulerWithConfig(config DogeSchedulerConfig) error {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	if sm.dogeScheduler.IsRunning() {
		return fmt.Errorf("DOGE调度器已在运行中")
	}

	// 重新创建调度器以应用新配置
	sm.dogeScheduler = NewDogeTradeSchedulerWithConfig(config)
	return sm.dogeScheduler.Start()
}

// StopDogeScheduler 停止DOGE交易调度器
func (sm *SchedulerManager) StopDogeScheduler() {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	sm.dogeScheduler.Stop()
}

// GetDogeSchedulerStatus 获取DOGE调度器状态
func (sm *SchedulerManager) GetDogeSchedulerStatus() map[string]interface{} {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	return sm.dogeScheduler.GetStatus()
}

// IsDogeSchedulerRunning 检查DOGE调度器是否正在运行
func (sm *SchedulerManager) IsDogeSchedulerRunning() bool {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	return sm.dogeScheduler.IsRunning()
}

// StartTrxScheduler 启动TRX交易调度器
func (sm *SchedulerManager) StartTrxScheduler() error {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	if sm.trxScheduler.IsRunning() {
		return fmt.Errorf("TRX调度器已在运行中")
	}

	return sm.trxScheduler.Start()
}

// StopTrxScheduler 停止TRX交易调度器
func (sm *SchedulerManager) StopTrxScheduler() {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	sm.trxScheduler.Stop()
}

// GetTrxSchedulerStatus 获取TRX调度器状态
func (sm *SchedulerManager) GetTrxSchedulerStatus() map[string]interface{} {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	return sm.trxScheduler.GetStatus()
}

// IsTrxSchedulerRunning 检查TRX调度器是否正在运行
func (sm *SchedulerManager) IsTrxSchedulerRunning() bool {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	return sm.trxScheduler.IsRunning()
}

// StopAllSchedulers 停止所有调度器
func (sm *SchedulerManager) StopAllSchedulers() {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	fmt.Println("[INFO] 正在停止所有调度器...")
	sm.dogeScheduler.Stop()
	sm.trxScheduler.Stop()
	fmt.Println("[SUCCESS] 所有调度器已停止")
}

// GetAllSchedulersStatus 获取所有调度器状态
func (sm *SchedulerManager) GetAllSchedulersStatus() map[string]interface{} {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	return map[string]interface{}{
		"doge_scheduler": sm.dogeScheduler.GetStatus(),
		"trx_scheduler":  sm.trxScheduler.GetStatus(),
	}
}

// GetDogeBuyPauseStatus 获取DOGE调度器买单暂停状态
func (sm *SchedulerManager) GetDogeBuyPauseStatus() (map[string]interface{}, error) {
	sm.mu.RLock()
	defer sm.mu.RUnlock()

	if sm.dogeScheduler == nil {
		return nil, fmt.Errorf("DOGE调度器未初始化")
	}

	return sm.dogeScheduler.getBuyPauseStatus(), nil
}

// ResumeDogeBuyOrders 手动恢复DOGE调度器买单
func (sm *SchedulerManager) ResumeDogeBuyOrders() error {
	sm.mu.Lock()
	defer sm.mu.Unlock()

	if sm.dogeScheduler == nil {
		return fmt.Errorf("DOGE调度器未初始化")
	}

	sm.dogeScheduler.resumeBuyOrders()
	return nil
}